<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
111
</body>
<script>
  // 输入：s = "babad"
  // 输出："bab"
  // 解释："aba" 同样是符合题意的答案。

  const fn = (s) => {
    let res = ''
    for (let i = 0; i < s.length; i++) {
      // 寻找长度为奇数的回文子串(以当前元素向两边扩散)
      const s1 = fn1(s, i, i)
      // 寻找长度为偶数的回文子串(以s[i],s[i + 1])向两边扩散
      const s2 = fn1(s, i, i + 1)
      res = res.length > s1.length ? res : s1
      res = res.length > s2.length ? res : s2
    }
    return res
  }

  const fn1 = (s, left, right) => {
    while (left >= 0 && right < s.length && s[left] === s[right]) {
      left--
      right++
    }
    console.log(right, left)
    // console.log(left + 1, right - left -1)
    // console.log(s.substr(left + 1, right - left - 1))
    return s.substr(left + 1, right - left - 1)
  }

  s = 'babad'
  console.log(fn(s))
</script>
</html>
